package com.example.designpatterns.prototype;

/**
 * 原型模式演示类
 */
public class PrototypeDemo {
    public static void main(String[] args) {
        // 浅克隆演示
        System.out.println("===== 浅克隆演示 =====");
        ConcretePrototype prototype1 = new ConcretePrototype("1", "原型对象1", "分类A");
        System.out.println("原始对象: " + prototype1);
        
        ConcretePrototype clonedPrototype1 = (ConcretePrototype) prototype1.clone();
        System.out.println("克隆对象: " + clonedPrototype1);
        
        // 修改克隆对象的属性
        clonedPrototype1.setName("克隆对象1");
        System.out.println("修改后的克隆对象: " + clonedPrototype1);
        System.out.println("原始对象: " + prototype1); // 原始对象不受影响
        
        // 深克隆演示
        System.out.println("\n===== 深克隆演示 =====");
        InnerObject innerObject = new InnerObject("内部值");
        DeepConcretePrototype prototype2 = new DeepConcretePrototype("2", "原型对象2", innerObject);
        System.out.println("原始对象: " + prototype2);
        
        DeepConcretePrototype clonedPrototype2 = (DeepConcretePrototype) prototype2.clone();
        System.out.println("克隆对象: " + clonedPrototype2);
        
        // 修改克隆对象的引用类型属性
        clonedPrototype2.getInnerObject().setValue("修改后的内部值");
        System.out.println("修改后的克隆对象: " + clonedPrototype2);
        System.out.println("原始对象: " + prototype2); // 原始对象不受影响
        
        // 使用原型注册表
        System.out.println("\n===== 原型注册表演示 =====");
        PrototypeRegistry registry = new PrototypeRegistry();
        
        // 注册原型对象
        registry.registerPrototype("prototype1", prototype1);
        registry.registerPrototype("prototype2", prototype2);
        
        // 从注册表获取克隆
        ConcretePrototype clonedFromRegistry1 = (ConcretePrototype) registry.getClone("prototype1");
        System.out.println("从注册表克隆的对象1: " + clonedFromRegistry1);
        
        DeepConcretePrototype clonedFromRegistry2 = (DeepConcretePrototype) registry.getClone("prototype2");
        System.out.println("从注册表克隆的对象2: " + clonedFromRegistry2);
    }
} 